package com.trinity.web.controller.report;

import com.trinity.campus.domain.CampusStudents;
import com.trinity.campus.service.ICampusStudentsService;
import com.trinity.common.core.controller.BaseController;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.common.core.domain.entity.SysDept;
import com.trinity.common.utils.StringUtils;
import com.trinity.common.utils.bean.BeanUtils;
import com.trinity.orders.constant.OrderConstants;
import com.trinity.orders.domain.XcOrders;
import com.trinity.orders.service.IXcOrdersService;
import com.trinity.orders.service.IXcPayRecordService;
import com.trinity.specialpay.domain.CampusActivityStudent;
import com.trinity.specialpay.dto.QueryActivityStudentsRequestDto;
import com.trinity.specialpay.service.ICampusActivityStudentService;
import com.trinity.system.service.ISysDeptService;
import com.trinity.uniquefees.domain.bo.UfCourseBook;
import com.trinity.uniquefees.dto.DeptSemesterStuCourseDto;
import com.trinity.uniquefees.service.IUfStudentBookService;
import com.trinity.web.controller.report.dto.PaymentStatusResultDto;
import com.trinity.web.controller.report.dto.PaymentStatusResultDto2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 缴费项目统计结果
 *
 * @author dsp
 */
@RestController
@RequestMapping("/report/payment")
public class PaymentStatus extends BaseController {

    @Resource
    private IXcOrdersService ordersService;
    @Resource
    private IXcPayRecordService payRecordService;

    @Resource
    private IUfStudentBookService ufStudentBookService;
    @Resource
    private ICampusActivityStudentService activityStudentService;
    @Resource
    private ICampusStudentsService studentsService;
    @Resource
    private ISysDeptService deptService;

    /**
     * 查询统计
     */
//    @PreAuthorize("@ss.hasPermi('report:finance:order')")
    @RequestMapping(value = "/status", method = {RequestMethod.GET})
    public AjaxResult list(QueryActivityStudentsRequestDto requestDto) {
        PaymentStatusResultDto financialStatementsResultDto = new PaymentStatusResultDto();
        List<CampusActivityStudent> campusActivityStudents = activityStudentService.selectActivityStudentOrderRecordList(requestDto);
        financialStatementsResultDto.setCampusActivityStudents(campusActivityStudents);
        SysDept sysDept = deptService.selectDeptById(requestDto.getDeptId());
        financialStatementsResultDto.setSchool(sysDept);
        //统计当前学生数量
        CampusStudents campusStudents = new CampusStudents();
        BeanUtils.copyBeanProp(campusStudents, requestDto);
        campusStudents.setStatus("0");
        Long studentNum = studentsService.countStudents(campusStudents);
        financialStatementsResultDto.setTotal(studentNum);
        //下单总人数
        int size = campusActivityStudents.size();
        financialStatementsResultDto.setTotalAct((long) size);
        //已交付人数
        long complete = campusActivityStudents.stream()
                .filter(ac -> "SUCCESS".equals(ac.getOrders().getStatus()))
                .count();
        financialStatementsResultDto.setCompleted(complete);
        //未缴费人数
        long incomplete = campusActivityStudents.stream()
                .filter(ac -> !"SUCCESS".equals(ac.getOrders().getStatus()))
                .count();
        financialStatementsResultDto.setIncomplete(incomplete);
        //完成百分比
        double successPercentage = (double) complete / size * 100.0;
        financialStatementsResultDto.setPercentage(String.valueOf(successPercentage));
        return success(financialStatementsResultDto);
    }

    /**
     * 统计具体的缴费项目进度
     */
//    @PreAuthorize("@ss.hasPermi('report:finance:order')")
    @RequestMapping(value = "/count", method = {RequestMethod.GET})
    public AjaxResult listNew(UfCourseBook book) {
        PaymentStatusResultDto2 result = new PaymentStatusResultDto2();
        //获取缴费名单
        DeptSemesterStuCourseDto suCourseDto = ufStudentBookService.selectUfDeptSemesterStuCourse(book);
        BeanUtils.copyBeanProp(result, suCourseDto);

        //获取订单
        XcOrders xcOrder = new XcOrders();
        xcOrder.setOther1(book.getScheduleId());
        List<XcOrders> xcOrders = ordersService.selectXcOrdersList(xcOrder);
        result.setOrders(xcOrders);

        //统计当前学生数量
        long stuCount = suCourseDto.getCourseStu() == null ? 0 : suCourseDto.getCourseStu().size();
        result.setTotal(stuCount);
        //下单总人数
        long oderCount = suCourseDto.getCourseStu() == null ? 0 : suCourseDto.getCourseStu().stream()
                .filter(oderP -> (StringUtils.isBlank(oderP.getTotalPrice()) ? 0.00 : Double.parseDouble(oderP.getTotalPrice())) >= 0)
                .count();
        result.setTotalAct(oderCount);
        //已交付人数
        long complete = xcOrders == null ? 0 : xcOrders.stream()
                .filter(order -> OrderConstants.TradeState.SUCCESS.equals(order.getStatus()))
                .count();
        result.setCompleted(complete);
        //未缴费人数
        long incomplete = stuCount - complete;
        result.setIncomplete(incomplete);
        //完成百分比
        double successPercentage = stuCount == 0 ? 0 : ((double) complete / stuCount * 100.0);
        result.setPercentage(String.valueOf(successPercentage));
        return success(result);
    }


}
